"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:SignChooselocationCtrl
 * @description
 * # SignChooselocationCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "SignChooselocationCtrl",
    function($scope, Util, localStorageService, MapService) {
      // 默认地址坐标
      var vm = this;
      var param = localStorageService.get("signparam")||{};
      // 默认地址坐标
      vm.point = {
        longitude: 115.863834,
        latitude: 28.68003
      };
      this.ak = MapService.getAk();

      this.locit = function(pos, index) {
        vm.posindex = index;
        vm.positon ={};
        vm.positon.point = {};
        vm.positon.point.latitude = pos.point.lat;
        vm.positon.point.longitude = pos.point.lng;
        vm.positon.address = pos.address;
        vm.positon.title = pos.title;
        MapService.setMarker(vm.map, vm.mapOptions, pos.point,vm.previousMarkers);
      };
      // 设置考勤位置
      this.setCheckPos = function() {
        console.log(vm.positon);
        if(!vm.positon){
          Util.Toast("请选择位置信息");
          return;
        }
        param.point = vm.positon.point;
        param.address = vm.positon.address+(vm.positon.title?"("+vm.positon.title+")":"");
        param.title = vm.positon.title;
        localStorageService.set("signparam", param);
        Util.goBack();
      };
      // 初始化成功后将map和标记返回以便获取具体位置
      this.finishmap = function(map, previousMarkers) {
        vm.map = map;
        vm.previousMarkers = previousMarkers;
        MapService.getcurPositon().then(function(data) {
          vm.point.latitude=vm.point.lat = data.latitude;
          vm.point.longitude=vm.point.lng = data.longitude;
          vm.mapOptions.markers = [vm.point];
          displayPOI();
        });
      };
      var displayPOI = function() {
        var mOption = {
          poiRadius: 500, //半径为500米内的POI,默认100米
          numPois: 20 //列举出50个POI,默认20个
        };
        var pointB = new BMap.Point(vm.point.longitude, vm.point.latitude);
        // var circle = new BMap.Circle(pointB, 500, { fillColor: "blue", strokeWeight: 1, fillOpacity: 0.3, strokeOpacity: 0.3 });
        // vm.map.addOverlay(circle);
        var myGeo = new BMap.Geocoder(); //创建地址解析实例
        myGeo.getLocation(
          pointB,
          function mCallback(rs) {
            vm.allPois = rs.surroundingPois; //获取全部POI（该点半径为100米内有6个POI点）
            $scope.$apply();
          },
          mOption
        );
      };
      // 初始化地图
      var initMap = function() {
        vm.offlineOpts = { retryInterval: 5000 };
        vm.mapOptions = {
          center: vm.point,
          zoom: 16,
          city: "NanChang",
          enableDragging: false
        };
      };
      initMap();
    }
  );
